package com.taotao.chatbot.service.impl;


import com.taotao.chatbot.domain.ChatMessageRequest;
import com.taotao.chatbot.domain.MedDoctor;
import com.taotao.chatbot.domain.MedMessageLog;
import com.taotao.chatbot.mapper.MedDoctorMapper;
import com.taotao.chatbot.mapper.MedMedicalRecordsMapper;
import com.taotao.chatbot.mapper.MedMessageLogMapper;
import com.taotao.chatbot.service.ChatService;
import com.taotao.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 聊天服务实现类
 */
@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private MedMessageLogMapper messageLogMapper;

    @Autowired
    private MedDoctorMapper doctorMapper;

    @Autowired
    private MedMedicalRecordsMapper medicalRecordsMapper;

    @Autowired
    private RedisService redisService;


    @Override
    @Transactional
    public void saveMessage(ChatMessageRequest request) {
        //从redis中获取挂号id
        String medicalRecordsId = (String) redisService.redisTemplate.opsForValue().get(request.getTraceNo());

        request.setMedicalRecordId(medicalRecordsId);

        messageLogMapper.insert(request);
    }

    @Override
    public List<MedMessageLog> getChatHistory(Long medicalRecordsId) {
        return messageLogMapper.selectByMedicalRecordsId(medicalRecordsId);
    }

    @Override
    public Long validateDoctor(String realName) {
        MedDoctor doctor = doctorMapper.selectByRealName(realName);
        return doctor != null ? doctor.getDoctorId() : null;
    }

    @Override
    @Transactional
    public void updateConsultationStatus(String medicalRecordId, Integer status) {
        //从redis中获取挂号id
        String medicalRecordsId = (String) redisService.redisTemplate.opsForValue().get(medicalRecordId);
        medicalRecordsMapper.updateStatus(medicalRecordsId, status) ;
    }

    @Override
    public Integer getStatus(String medicalRecordId) {
        //从redis中获取挂号id
        String medicalRecordsId = (String) redisService.redisTemplate.opsForValue().get(medicalRecordId);
        return medicalRecordsMapper.getStatus(medicalRecordsId);
    }
}
